package com.myhkvideo.yulongbpcdemo.service;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.myhkvideo.yulongbpcdemo.entity.MetalAndGradeAndRateDayInfoData;
import com.myhkvideo.yulongbpcdemo.mapper.MetalAndGradeAndRateDayInfoMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpMethod;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Service;
import org.springframework.web.client.RestTemplate;

import java.io.IOException;
import java.math.BigDecimal;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.UUID;

@Service
public class MetalAndGradeAndRateDayInfoService {

    private static final String API_URL = "http://ylty.westmining.com:60016/api/BoardProductionCommand/MetalAndGradeAndRateDayInfo";
    @Autowired
    private LoginService loginService;

    @Autowired
    private RestTemplate restTemplate;

    @Autowired
    private ObjectMapper objectMapper;

    @Autowired
    private MetalAndGradeAndRateDayInfoMapper metalAndGradeAndRateDayInfoMapper;

    public ResponseEntity<String> getMetalAndGradeAndRateDayInfo() {
        String token = loginService.getNewToken();
        HttpHeaders headers = new HttpHeaders();
        headers.add("Content-Type", "application/json");
        headers.add("Authorization", "Bearer " + token);
        HttpEntity<String> requestEntity = new HttpEntity<>(headers);
        return restTemplate.exchange(API_URL, HttpMethod.POST, requestEntity, String.class);
    }

    /**
     * 获取并转换数据为 MetalAndGradeAndRateDayInfoData 列表
     * @return MetalAndGradeAndRateDayInfoData 列表
     * @throws IOException
     * @throws ParseException
     */
    public List<MetalAndGradeAndRateDayInfoData> getAndConvertMetalAndGradeAndRateDayInfo() throws IOException, ParseException {
        ResponseEntity<String> response = getMetalAndGradeAndRateDayInfo();
        Map<String, Object> responseBody = objectMapper.readValue(response.getBody(), Map.class);
        List<Map<String, Object>> dataList = (List<Map<String, Object>>) responseBody.get("Data");
        return convertToMetalAndGradeAndRateDayInfoDataList(dataList);
    }

    private List<MetalAndGradeAndRateDayInfoData> convertToMetalAndGradeAndRateDayInfoDataList(List<Map<String, Object>> dataList) throws ParseException {
        List<MetalAndGradeAndRateDayInfoData> resultList = new ArrayList<>();
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
        for (Map<String, Object> dataMap : dataList) {
            MetalAndGradeAndRateDayInfoData data = new MetalAndGradeAndRateDayInfoData();
            data.setCreateTime(dateFormat.parse((String) dataMap.get("createtime")));
            data.setD_ycjjsl_cu(BigDecimal.valueOf((Double) dataMap.get("d_ycjjsl_cu")));
            data.setD_ycjjsl_mo(BigDecimal.valueOf((Double) dataMap.get("d_ycjjsl_mo")));
            data.setD_ecjjsl_cu(BigDecimal.valueOf((Double) dataMap.get("d_ecjjsl_cu")));
            data.setD_ecjjsl_mo(BigDecimal.valueOf((Double) dataMap.get("d_ecjjsl_mo")));
            data.setD_ylcjsl(BigDecimal.valueOf((Double) dataMap.get("d_ylcjsl")));
            data.setD_total_cu(BigDecimal.valueOf((Double) dataMap.get("d_total_cu")));
            data.setD_total_mo(BigDecimal.valueOf((Double) dataMap.get("d_total_mo")));
            data.setD_ycjjkpw_cu(BigDecimal.valueOf((Double) dataMap.get("d_ycjjkpw_cu")));
            data.setD_ycjjkpw_mo(BigDecimal.valueOf((Double) dataMap.get("d_ycjjkpw_mo")));
            data.setD_ecjjkpw_cu(BigDecimal.valueOf((Double) dataMap.get("d_ecjjkpw_cu")));
            data.setD_ecjjkpw_mo(BigDecimal.valueOf((Double) dataMap.get("d_ecjjkpw_mo")));
            data.setD_ycjhsl_cu(BigDecimal.valueOf((Double) dataMap.get("d_ycjhsl_cu")));
            data.setD_ycjhsl_mo(BigDecimal.valueOf((Double) dataMap.get("d_ycjhsl_mo")));
            data.setD_ecjhsl_cu(BigDecimal.valueOf((Double) dataMap.get("d_ecjhsl_cu")));
            data.setD_ecjhsl_mo(BigDecimal.valueOf((Double) dataMap.get("d_ecjhsl_mo")));
            resultList.add(data);
        }
        return resultList;
    }


}